package com.example.zg.learn;

import android.animation.ObjectAnimator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.res.AssetManager;
import android.graphics.Point;
import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.NumberPicker;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.example.zg.learn.customView.ProgressView;
import com.example.zg.learn.evaluator.ProgressTypeEvaluator;

import java.io.InputStream;

public class SimpleUIActivity extends AppCompatActivity {
    private static final String TAG = "SimpleUIActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_ui);

//        test();

        setEdit();
        setRadio();
        setSpinner();
        setNumberPick();
        LayoutInflater.from(this).inflate(R.layout.fragment_guanzhu,null,false);
//        setListViewAnim();

        setProgressView();
    }

    private void setProgressView(){
        /*ProgressView progressView=findViewById(R.id.progressView);
        ObjectAnimator objectAnimator=ObjectAnimator.ofObject(progressView,"point",new ProgressTypeEvaluator());
        objectAnimator.setInterpolator(new LinearInterpolator());
        objectAnimator.setObjectValues(new Point());*/
    }

    private void setListViewAnim(){
        ListView listView=findViewById(R.id.listView);
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.tran);
        LayoutAnimationController laController=new LayoutAnimationController(animation);
        laController.setDelay(0.8f);//子组件动画执行的时间间隔,单位秒,0.8秒
        laController.setOrder(LayoutAnimationController.ORDER_NORMAL);//子组件动画执行顺序
        listView.setLayoutAnimation(laController);
    }
    private void setNumberPick(){
        NumberPicker numberPicker=findViewById(R.id.numberPicker);
        numberPicker.setMinValue(0);
        numberPicker.setMaxValue(10);
        numberPicker.setValue(2);

    }

    private void setSpinner(){
        Spinner spinner=findViewById(R.id.spinner);
        spinner.setSelection(2);
    }

    private void setRadio(){
        RadioGroup radioGroup=findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            Toast.makeText(SimpleUIActivity.this, ""+checkedId, Toast.LENGTH_SHORT).show();
            switch (checkedId){
                case R.id.radioButton1:
                    break;
                    //.....
            }
        });
    }

    private void setEdit(){
        EditText editText=findViewById(R.id.editText);
        editText.setFilters(new InputFilter[]{new InputFilter.AllCaps(),new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                Log.i(TAG, "filter: source="+source);
                Log.i(TAG, "filter: start="+start);
                Log.i(TAG, "filter: end="+end);
                Log.i(TAG, "filter: dest="+dest);
                Log.i(TAG, "filter: dstart="+dstart);
                Log.i(TAG, "filter: dend="+dend);
                if("1".equals(source.toString())){
                    return "一";
                }else if("2".equals(source.toString())){
                    return "二";
                }
                return source;
            }
        }});

    }

    class Book {
        int price=50;
    }

    class IntegerTypeEvaluator implements TypeEvaluator<Integer> {

        @Override
        public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
            //参数fraction：动画完成度。
            //startValue 动画开始值
            //endValue 动画结束值
            Log.i(TAG, "evaluate: endValue="+endValue);
            Log.i(TAG, "evaluate: startValue="+startValue);
            Log.i(TAG, "evaluate: fraction="+fraction);

            return (int) (startValue+(endValue - startValue) * fraction);
        }
    }

    public void test(View view){
        //属性动画

        ValueAnimator animator=ValueAnimator.ofInt(1,10,5);
        animator.setDuration(1000);
        animator.setEvaluator(new IntegerTypeEvaluator());
        animator.addUpdateListener(animation->{
            int v= (int) animation.getAnimatedValue();
            Log.i(TAG, "test: v="+v);
        });

        animator.start();

        Log.i(TAG, "test: >>>>>>>>>>>>>>>>>>>>>>>>>>>");
       /* ImageView imageView=findViewById(R.id.image1);
        AnimationDrawable drawable= (AnimationDrawable) imageView.getDrawable();
        drawable.start();*/

        //点击按钮,自身向右移动200下移动100
        //默认坐标类型为ABSOLUTE:即像素
       /* TranslateAnimation trans=new TranslateAnimation(
                TranslateAnimation.RELATIVE_TO_PARENT,0,
                TranslateAnimation.RELATIVE_TO_PARENT,0.3f,
                TranslateAnimation.RELATIVE_TO_PARENT,0,
                TranslateAnimation.RELATIVE_TO_PARENT,0.2f);
        trans.setDuration(2000);//动画执行时间
//        trans.setRepeatCount(2);//重复次数
        trans.setRepeatMode(Animation.RESTART);//重复类型:重新开始执行
            trans.setFillAfter(true);//组件停止在动画后的位置
//            trans.setFillBefore(true);//默认返回原来位置

*/
/*
        AlphaAnimation alpha=new AlphaAnimation(1,0);
        alpha.setDuration(2000);
        view.startAnimation(alpha);*/

       /* ScaleAnimation scale=new ScaleAnimation(
                1,//fromX动画前组件宽度的比例
                2,//toX动画后组件宽度的比例
                1,//fromY动画前组件高度的比例
                2,//toY动画后组件高度的比例
                ScaleAnimation.RELATIVE_TO_SELF,//pivotXType坐标原点X的类型
                0.5f,//pivotXValue以哪个x坐标为圆点开始缩放
                ScaleAnimation.RELATIVE_TO_SELF,//pivotYType坐标原点Y的类型
                0.5f);//pivotYValue以哪个Y坐标为圆点开始缩放
        scale.setDuration(3000);
        view.startAnimation(scale);*/

     /*   RotateAnimation rotate=new RotateAnimation(
                0f,//开始角度
                180f,//结束角度
                RotateAnimation.RELATIVE_TO_SELF, 0.5f,//圆心x
                RotateAnimation.RELATIVE_TO_SELF, 0.5f);//圆心y

        rotate.setDuration(3000);
        view.startAnimation(rotate);*/
       /* Animation trans = AnimationUtils.loadAnimation(this, R.anim.tran);

        view.startAnimation(trans);*/



      /*  ImageView image1=findViewById(R.id.image1);
        int imageViewWidth = image1.getWidth();
        int imageViewHeight=image1.getHeight();
        Log.i(TAG, "imageViewWidth: "+imageViewWidth);
        Log.i(TAG, "imageViewHeight: "+imageViewHeight);
        Rect bounds = image1.getDrawable().getBounds();
        int imageWidth=bounds.width();
        int imageHeight=bounds.height();
        Log.i(TAG, "imageWidth:真实 "+imageWidth);
        Log.i(TAG, "imageHeight:真实 "+imageHeight);

        Matrix imageMatrix = image1.getImageMatrix();
        float[] values=new float[9];
        imageMatrix.getValues(values);
        Log.i(TAG, "MSCALE_X: "+values[Matrix.MSCALE_X]);
        Log.i(TAG, "MSCALE_Y: "+values[Matrix.MSCALE_Y]);*/



    }


    public void test2(View view) {
        ToggleButton toggleButton= (ToggleButton) view;

    }
}
